package gui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * Starting GUI when the user starts the program.
 * @author Daniel Anderson and Luc H. Le
 * @version TCSS422 2012
 */
@SuppressWarnings("serial")
public class StartFrame extends JFrame {

	private JFrame my_frame;
	private JLabel my_label;
	private JPanel my_panel;
	private JPanel my_text_panel;
	private JTextField my_url_field;
	private JTextField my_keyword_field;
	private JTextField my_limit_field;
	
	public StartFrame() {
		super();
		my_frame = new JFrame("TCSS 422 Webcrawler");
		my_label = new JLabel("Select Crawler Type:");
		my_panel = new JPanel();
		my_text_panel = new JPanel();
		my_url_field = new JTextField("Enter initial URL to crawl");
		my_keyword_field = new JTextField("Enter keywords seperated by whitespace");
		my_limit_field = new JTextField("Enter the limit of pages to crawl");
	}
	
	public void start() {
		my_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		my_frame.setLayout(new BoxLayout(my_frame.getContentPane(), BoxLayout.PAGE_AXIS));
		my_frame.add(my_label);
		my_frame.add(my_panel);
		my_frame.add(my_text_panel);
		my_panel.setLayout(new FlowLayout());
		my_text_panel.setLayout(new BoxLayout(my_text_panel, BoxLayout.PAGE_AXIS));
		JButton threaded = new JButton("Multi-Threaded");
		JButton nonthreaded = new JButton("Non-Threaded");
		my_panel.add(threaded);
		my_panel.add(nonthreaded);
		my_text_panel.add(my_keyword_field);
		my_text_panel.add(my_url_field);
		my_text_panel.add(my_limit_field);
		
		threaded.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(checkInput()) {
					ThreadedCrawlerFrame nextframe = new ThreadedCrawlerFrame(getKeywords(), my_url_field.getText(), Integer.parseInt(my_limit_field.getText()));
					nextframe.start();
					my_frame.dispose();
				}
			}
		});
		
		
		nonthreaded.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(checkInput()) {
					NormalCrawlerFrame nextframe = new NormalCrawlerFrame(getKeywords(), my_url_field.getText(), Integer.parseInt(my_limit_field.getText()));
					nextframe.start();
					my_frame.dispose();
				}
			}
		});
		
		my_frame.pack();
		//my_frame.setResizable(false);
		my_frame.setVisible(true);
		
	}
	
	private boolean checkInput() {
		boolean check = true;
		String text = my_url_field.getText();
		try {
			URL url = new URL(text);
			URLConnection c = url.openConnection();
			c.connect();
			Integer.parseInt(my_limit_field.getText());
		} catch (MalformedURLException e1) {
			check = false;
			JOptionPane.showMessageDialog(my_frame, "Invalid URL!",  "URL Error", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e2) {
			check = false;
			JOptionPane.showMessageDialog(my_frame, "Connection is Bad!",  "URL Error", JOptionPane.ERROR_MESSAGE);
		} catch (NumberFormatException e3) {
			check = false;
			JOptionPane.showMessageDialog(my_frame, "Limit is not an Integer!",  "Limit Error", JOptionPane.ERROR_MESSAGE);
		}
		
		return check;
	}
	
	private List<String> getKeywords() {
		List<String> keys = new ArrayList<String>();
		StringTokenizer t = new StringTokenizer(my_keyword_field.getText());
		while (t.hasMoreTokens()) {
			keys.add(t.nextToken());
		}
		return keys;
	}
}//end of StartFrame
